<template>
  <el-dialog
    :title="dlgTitle"
    :visible.sync="dlgVisible"
    :destroy-on-close="true"
    width="500px"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <div class="dlg_content">
      <el-form ref="formBatch" :model="formdata">
        <template v-if="batchSetType === 'nurseStation'">
          <el-form-item label="护士站" prop="nurseStation" label-width="70px">
            <el-input v-model="formdata.nurseStation"></el-input>
          </el-form-item>
        </template>
        <template v-if="batchSetType === 'checkGroup'">
          <el-form-item label="检查分组" prop="checkGroup" label-width="80px">
            <el-select
              style="width: 100%;"
              v-model="formdata.checkGroup"
              v-select-create
              filterable
              allow-create
              clearable
              default-first-option
              placeholder="请选择">
              <el-option
                v-for="(item,index) in checkGroupOptions"
                :key="index"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </template>
        <template v-if="batchSetType === 'modalityAddress'">
          <el-form-item label="检查地点" prop="modalityAddress" label-width="80px">
            <el-select
              style="width: 100%;"
              v-model="formdata.modalityAddress"
              v-select-create
              filterable
              allow-create
              clearable
              default-first-option
              placeholder="请选择">
              <el-option
                v-for="(item,index) in modalityAddressOptions"
                :key="index"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </template>
        <template v-if="batchSetType === 'examType'">
          <el-form-item label="检查类型" prop="" label-width="80px">
            <el-select
              style="width: 100%;"
              v-model="formdata.examType"
              v-select-create
              filterable
              allow-create
              clearable
              default-first-option
              placeholder="请选择">
              <el-option
                v-for="(item,index) in examTypeOptions"
                :key="index"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </template>
        <template v-if="batchSetType === 'area'">
          <el-form-item label="医院院区代码" prop="areaNo" label-width="110px">
            <el-input v-model="formdata.areaNo"></el-input>
          </el-form-item>
          <el-form-item label="医院院区名称" prop="areaName" label-width="110px">
            <el-input v-model="formdata.areaName"></el-input>
          </el-form-item>
        </template>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="dlgVisible = false">取消</el-button>
      <el-button size="small" type="primary" @click="sendSubmit">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  postcheckroomsetnursestation,
  postcheckroomsetmodalityaddress,
  postcheckroomsetareaname,
  getlistbymodalityaddress,
  postcheckroomsetcheckgroup,
  getCheckGroupList,
  getExamList,
  postsetexamtype
} from '@/api/api'
export default {
  props: {
    hospitalCode: {
      type: String,
      default: ''
    },
    dep: {
      type: Array,
      default: () => []
    },
    ids: {
      type: Array,
      default: () => []
    },
    batchSetType: {
      // type : area（院区）、modalityAddress（机房地址）、nurseStation（护士站）、checkGroup（机房分组）
      type: String,
      default: ''
    },
    batchSetDlg: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dlgTitle: '批量设置',
      formdata: {
        checkGroup: '', // 机房分组
        nurseStation: '', // 护士站
        modalityAddress: '', // 机房地址
        areaNo: '', // 医院院区代码
        areaName: '', // 医院院区名称
        examType: ''
      },

      modalityAddressOptions: [],
      checkGroupOptions: [],
      examTypeOptions: []
    }
  },
  computed: {
    dlgVisible: {
      get() {
        return this.batchSetDlg
      },
      set(val) {
        this.$emit('update:batchSetDlg', val)
      }
    }
  },
  watch: {
    dlgVisible: {
      handler(val) {
        if (val && this.batchSetType === 'modalityAddress') {
          this.getModalityAddressOptions()
        }
        if (val && this.batchSetType === 'checkGroup') {
          this.getCheckGroupOptions()
        }
        if (!val) {
          this.$refs.formBatch && this.$refs.formBatch.resetFields()
        }
        if (val && this.batchSetType === 'examType') {
          this.getExamTypeOptions()
        }
      },
      immediate: true
    }
  },
  methods: {
    sendSubmit() {
      if (this.batchSetType === 'nurseStation') {
        this.sendNurseStation()
      } else if (this.batchSetType === 'modalityAddress') {
        this.sendModalityAddress()
      } else if (this.batchSetType === 'area') {
        this.sendArea()
      } else if (this.batchSetType === 'checkGroup') {
        this.sendCheckgroup()
      } else if (this.batchSetType === 'examType') {
        this.sendExamType()
      }
    },
    // 设置检查类型
    sendExamType() {
      const params = {
        hospitalCode: this.hospitalCode,
        ids: this.ids,
        examType: this.formdata.examType
      }
      postsetexamtype(params).then(res => {
        if (res && res.code === 10000) {
          this.dlgVisible = false
          this.$message.success(res.message || '批量设置检查类型成功')
          this.$emit('refresh')
        }
      })
    },
    // 设置机房分组
    sendCheckgroup() {
      const params = {
        hospitalCode: this.hospitalCode,
        ids: this.ids,
        checkGroup: this.formdata.checkGroup
      }
      postcheckroomsetcheckgroup(params).then(res => {
        if (res && res.code === 10000) {
          this.dlgVisible = false
          this.$message.success(res.message || '批量设置检查分组成功')
          this.$emit('refresh')
        }
      })
    },
    // 设置护士站
    sendNurseStation() {
      const params = {
        hospitalCode: this.hospitalCode,
        ids: this.ids,
        nurseStation: this.formdata.nurseStation
      }
      postcheckroomsetnursestation(params).then(res => {
        if (res && res.code === 10000) {
          this.dlgVisible = false
          this.$message.success(res.message || '批量设置护士站成功')
          this.$emit('refresh')
        }
      })
    },
    // 设置机房地址
    sendModalityAddress() {
      const params = {
        hospitalCode: this.hospitalCode,
        ids: this.ids,
        modalityAddress: this.formdata.modalityAddress
      }
      postcheckroomsetmodalityaddress(params).then(res => {
        if (res && res.code === 10000) {
          this.dlgVisible = false
          this.$message.success(res.message || '批量设置检查地点成功')
          this.$emit('refresh')
        }
      })
    },
    // 设置院区
    sendArea() {
      const params = {
        hospitalCode: this.hospitalCode,
        ids: this.ids,
        areaNo: this.formdata.areaNo,
        areaName: this.formdata.areaName
      }
      postcheckroomsetareaname(params).then(res => {
        if (res && res.code === 10000) {
          this.dlgVisible = false
          this.$message.success(res.message || '批量设置院区成功')
          this.$emit('refresh')
        }
      })
    },
    getModalityAddressOptions() {
      getlistbymodalityaddress({ hospitalCode: this.hospitalCode }).then(res => {
        if (res && res.code === 10000) {
          this.modalityAddressOptions = res.content.list
        }
      })
    },
    getCheckGroupOptions() {
      let data = {
        HospitalCode: this.hospitalCode,
        dep: this.dep.join(',')
      }
      return new Promise(async resolve => {
        await getCheckGroupList(data).then(res => {
          if (res && res.code === 10000) {
            this.checkGroupOptions = res.content.list
            resolve(res)
          }
        })
      })
    },
    getExamTypeOptions() {
      const params = {
        hospitalCode: this.hospitalCode,
        areaName: this.formdata.areaName
      }
      return new Promise(resolve => {
        getExamList(params).then(res => {
          if (res.code === 10000) {
            this.examTypeOptions = res.content.list
            resolve(res)
          }
        })
      })
    },
    handleClose() {
      this.dlgVisible = false
    }
  }
}
</script>

<style>

</style>
